package com.fm.demo.utils

import android.graphics.Rect
import androidx.recyclerview.widget.RecyclerView
import android.view.View

/**
 * Created by lyy  on 2018/8/31.
 */
class SpaceItemDecoration(
        //列数
        private val spanCount: Int,
        //间隔
        private val space: Int) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        //这里是关键，需要根据你有几列来判断
        val position = parent.getChildAdapterPosition(view) // item position
        val column = position % spanCount // item column
        if (spanCount <= 1) {
            if (parent.getChildAdapterPosition(view) != 0) {
                outRect.top = space
            }
            return
        }
        outRect.left = column * space / spanCount // column * ((1f / spanCount) * spacing)
        outRect.right = space - (column + 1) * space / spanCount // spacing - (column + 1) * ((1f /    spanCount) * spacing)
    }
}